package com.example.java.controller;

import com.example.java.service.IToolService;
import com.example.java.utils.Result;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/tool")
public class ToolController {


    @Autowired
    private IToolService iToolService;

    @RequestMapping(value = "/oilprice", method = RequestMethod.POST)
    public Result oilPrice(@RequestBody Map<String, Object> data) {
        System.out.println(data);
        String result = iToolService.oilPrice(data.get("city").toString());
        Map<String,Object> res = new HashMap<>();
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode jsonNode = objectMapper.readTree(result);
            int code = jsonNode.get("code").asInt();
            if(code == 200){
                res.put("p0",jsonNode.get("result").get("p0").asText());
                res.put("p89",jsonNode.get("result").get("p89").asText());
                res.put("p92",jsonNode.get("result").get("p92").asText());
                res.put("p95",jsonNode.get("result").get("p95").asText());
                res.put("p98",jsonNode.get("result").get("p98").asText());
            }
            res.put("code",code);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Result.ok().data("data", res);
    }

}
